$(document).ready(function() {
	$("#addOperatorFormDiv").dialog({
		autoOpen : false,
		modal : true,
		close:function(){
			$("input[type='text']").val("");
			$("input[type='password']").val("");
			$("input[name='permissionID']").attr("checked", false);
			$("#operatorID").val("-1");
		}
	});

	$("#addOperatorButton").click(function() {
		$("#addOperatorFormDiv").dialog("open");
	});

	$("#saveUpdateButton").click(function() {
		if($("#name").val()=="" || $("#userName").val()=="" || $("#password").val()==""){
			alert("所有项是必填的");
			return;
		}
		if($("#password").val()!=$("#repassword").val()){
			alert("两次密码不一致");
			return;
		}
		if($("input:checked").length==0){
			alert("至少选一个权限");
			return;
		}
		
		$("#saveUpdateButton").attr("disabled","disabled");
		var uri;
		if($("#operatorID").val()==-1){
			uri = "/MainMenu/configuration/insertOperator.htm";
		}else{
			uri = "/MainMenu/configuration/updateOperator.htm";
		}
		
		$.post(getRootPath()+ uri,
			   $("#addOperatorForm").serializeArray(),
			   function(data) {
					if(data.error==undefined){
						alert("保存成功!");
						$("#addOperatorFormDiv").dialog("close");
						$("#operatorsTable").load(getRootPath()+ "/MainMenu/configuration/operatorList.htm",{pageIndex:1});
					}else{
						alert(data.error);
					}
					$("#saveUpdateButton").removeAttr("disabled");
				});
	});
	
	$("#operatorsTable").load(getRootPath()+ "/MainMenu/configuration/operatorList.htm",{pageIndex:1});
	
});
